package com.thero.core.chess;

/**
 * Created by wusk on 2017/11/30.
 * 游戏过程中的消息结构
 */
public class Message {
    private short cmd;
    private short home;
    private int index;
    private byte[] information;

    /**
     * 游戏消息
     * @param code 命令字
     * @param home 发送到哪个进程，进程ID
     * @param size 消息体长度，即数据长度，不包括消息头
     */
    public Message(short code, short home, int size){
        this.cmd = code;
        this.home = home;
        this.index = 0;
        if(0 > size){
            information = new byte[size];
        }
    }

    public short getCmd(){
        return this.cmd;
    }

    public short getHome(){ return this.home; }

    public byte[] getInformation(){
        return this.information;
    }

    public void writeInt(int in){
        information[index] = (byte) (in & 0xff);
        information[index + 1] = (byte) ((in >> 8) & 0xff);
        information[index + 2] = (byte) ((in >> 16) & 0xff);
        information[index + 3] = (byte) (in >>> 24);
        this.index = this.index + 4;
    }

    public void writeShort(short in){
        int number = in;
        information[index] = new Integer(number & 0xff).byteValue();
        number = number >> 8;
        information[index + 1] = new Integer(number & 0xff).byteValue();
        this.index = this.index + 2;
    }
}
